﻿using System.Collections.Generic;
using System.Text;
using System.Collections;
using SZJT.Framework.Services;

namespace SZJT.Framework.Core
{
    public class XmlDataElement
    {
        public string Name
        {
            get;
            set;
        }

        public ServiceDataType ServiceDataType
        {
            get;
            set;
        }

        public string Value
        {
            get;
            set;
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            if (ServiceDataType != ServiceDataType.UNKNOWN)
            {
                sb.Append(
                    new XmlHelper.XmlElement(Name)
                        .AddAttribute(Constants.TypeName, ServiceDataType.ToString())
                    .ToString());
            }
            else
            {
                sb.Append(XmlHelper.GetElementString(Name));
            }

            sb.Append(System.Web.HttpUtility.HtmlEncode(Value));
            sb.Append(XmlHelper.GetEndElementString(Name));

            return sb.ToString();
        }

        /// <summary>
        /// 以“Data”节点标识的字段集合Xml节点
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        //public IList<XmlDataElement> GetXmlDataElementList(object collection)
        //{
        //    IList<XmlDataElement> list = new List<XmlDataElement>();
        //    foreach (var entity in (IEnumerable)collection)
        //    {
        //        EntitySchema entitySchema = EntitySchemaManager.GetEntitySchema(entity.GetType());
        //        foreach (var property in entitySchema.PropertyShcemaDictionary.Values)
        //        {
        //            if (property.ReservedColumnType == ReservedColumnType.None && property.ServiceToLoad)
        //            {
        //                XmlDataElement dataElement = new XmlDataElement();
        //                dataElement.Name = MessageHelper.GetServiceElementName(property.ColumnName);
        //                dataElement.ServiceDataType = property.ServiceDataType;
        //                object propertyValue = ReflectionHelper.GetProperty(property.ColumnName, entity);
        //                dataElement.Value = propertyValue != null ?
        //                    (propertyValue is bool ? propertyValue.ToString().ToUpper() : propertyValue.ToString())
        //                    : string.Empty;
        //                list.Add(dataElement);
        //            }
        //        }

        //    }
        //    return list;
        //}
    }
}
